package com.xingkeduo.business.operation.domain;

import com.google.common.collect.Lists;
import com.xingkeduo.business.operation.service.CountryService;
import com.xingkeduo.pojo.query.CountryQuery;
import com.xingkeduo.pojo.table.CountryTable;
import com.xingkeduo.util.pojo.exception.DataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class CountryDomain {

    @Autowired
    private CountryService countryService;

    public CountryTable getCountryById(Long id) throws Throwable {

        CountryTable countryTable = countryService.selectById(id);

        return countryTable;
    }

    public List<CountryTable> getCountryList(CountryQuery countryQuery) throws Throwable {

        List<CountryTable> countryList = countryService.selectLists(countryQuery);

        return countryList;
    }

    public List<CountryTable> getAllCountryByPid(Integer pid) throws Throwable {
        //1.获取顶级城市
        CountryQuery countryQuery = new CountryQuery();
        countryQuery.setPid(pid).setType(1);
        List<CountryTable> selectTopLists = countryService.selectLists(countryQuery);

        // 3.查询当前所有城市
        CountryQuery countryQuery1 = new CountryQuery();
        countryQuery1.setIsdelete(1);
        List<CountryTable> typePermsList = countryService.selectLists(countryQuery1);

        // 4.获取一级城市
        List<CountryTable> countryTableList = Lists.newArrayList();
        for (CountryTable entity : selectTopLists) {
            // 如果一级城市，则查询此城市下所有子城市
            List<CountryTable> childList = getChildCitys(entity.getId(), typePermsList);

            entity.setChildren(childList);
            countryTableList.add(entity);
        }

        return countryTableList;
    }

    // 递归获取子城市
    private List<CountryTable> getChildCitys(Long parentId, List<CountryTable> list) {
        List<CountryTable> childList = Lists.newArrayList();    // 子菜单
        for (CountryTable entity : list) {
            if (entity.getPid() == null) {
                continue;
            }
            if (entity.getPid().equals(parentId)) {
                childList.add(entity);
            }
        }

        for (CountryTable entity : childList) {
            entity.setChildren(getChildCitys(entity.getId(), list));
        }
        if (CollectionUtils.isEmpty(childList)) {
            return Collections.emptyList();
        }
        return childList;
    }


    /**
     * 查询城市名称
     *
     * @param name:
     * @return : com.xingkeduo.pojo.table.CountryTable
     * @author : gaozifeng
     * @date : 2019/4/17 16:45
     */
    public CountryTable getCityId(String name) throws Throwable {
        List<CountryTable> countryTables = countryService.selectLists(new CountryQuery().setName(name));
        if (null == countryTables || countryTables.size() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "城市不存在");
        }
        return countryTables.get(0);
    }
}
